<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('发生日期')" prop="occurrenceDate">
            <el-date-picker
              v-model="form.occurrenceDate"
              clearable
              style="width: 200px"
              type="date"
              value-format="yyyy-MM-dd"
              :placeholder="$t('发生日期')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('项目阶段')" prop="projectStage">
            <dict-select
              v-model="form.projectStage"
              dictionary="deal_stage_of_project"
              :placeholder="$t('项目阶段')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('费用类型')" prop="amountType">
            <dict-select
              v-model="form.amountType"
              dictionary="deal_amount_type"
              :placeholder="$t('费用类型')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('费用金额(元)')" prop="expenses">
            <Money v-model="form.expenses" :placeholder="$t('费用金额(元)')" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('费用说明')" prop="feeDescription">
            <el-input v-model="form.feeDescription" type="textarea" :placeholder="$t('费用说明')" show-word-limit maxlength="1000" />
          </el-form-item>
        </el-col>
      </el-row>
      <!-- 附件列表 -->
      <FileTable :list.sync="form.fileList" data-type="deal_business_expense" :business-id="form.dealId" :btn-show="!otherBtnHide" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="deal:expense" />
    </div>
  </div>
</template>

<script>
    import { saveDealBusinessExpense, getDealBusinessExpense } from '@/api/deal/expense/dealBusinessExpense'
    export default {
        data() {
            return {
                // 表单参数
                form: {},
                // 遮罩层
                loading: false,
                // 表单校验
                rules: {
                  occurrenceDate: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  projectStage: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  amountType: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }],
                  expenses: [{ required: true, message: this.$t('必填信息'),	trigger: 'blur' }]
                },
                // 是否隐藏其它按钮
                otherBtnHide: false
            }
        },
        created() {
            this.getDicts('deal_stage_of_project').then(response => {
                this.projectStageOptions = response.data
            })
            this.getDicts('deal_amount_type').then(response => {
                this.amountTypeOptions = response.data
            })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getDealBusinessExpense(data.id).then(response => {
                        this.form = response.data
                        this.loading = false
                    }).catch(() => {
                      this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    dealId: this.$route.query.id,
                    occurrenceDate: undefined,
                    projectStage: undefined,
                    amountType: undefined,
                    expenses: undefined,
                    feeDescription: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined
                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
                this.$refs['form'].validate(valid => {
                    if (valid) {
                        this.loading = true
                        saveDealBusinessExpense(this.form).then(response => {
                            if (response.code === 200) {
                                this.msgSuccess(this.$t('保存成功'))
                                this.closeForm()
                            } else {
                                this.msgError(response.msg)
                            }
                            this.loading = false
                        })
                    } else {
                       this.locationError(document)
                    }
                }).catch(() => {
                    this.loading = false
                })
           }
        }
    }
</script>
